#include "vector3d.h"
#include "vector.h"
#include <cstdlib>
#include <iostream>

Vector3D::Vector3D(){
	x = 0.0;
	y = 0.0;
	z = 0.0;
}

Vector3D::Vector3D(const Vector &v){
    if(v.getRow() != 3){
        std::cout <<"Erro no contrutor de Vector3D!"<<std::endl;
        exit(1);
    }
	x = v[0];
	y = v[1];
	z = v[2];
}

Vector3D::Vector3D(double _x, double _y, double _z):x(_x), y(_y), z(_z){
}

Vector3D::~Vector3D(){
}

double &Vector3D::operator[](unsigned int _i){
    switch(_i){
        case 0:
            return x;
            break;
        case 1:
            return y;
            break;
        case 2:
            return z;
            break;
    }
    std::cout <<"Erro no operador [] de Vector3D!"<<std::endl;
    exit(1);
}

Vector3D& Vector3D::operator=(const Vector3D &_vector){
	if(this != &_vector){
		x = _vector.x;
		y = _vector.y;
		z = _vector.z;
	}
	return *this;
}

Vector3D Vector3D::operator*(double _s) const {
	return Vector3D(x*_s, y*_s, z*_s);
}

Vector3D Vector3D::operator+(const Vector3D &_vector) const {
	return Vector3D(x+_vector.x, y+_vector.y, z+_vector.z);
}

Vector3D Vector3D::operator-(const Vector3D &_vector) const {
	return Vector3D(x-_vector.x, y-_vector.y, z-_vector.z);
}

Vector3D Vector3D::crossProduct(const Vector3D &_vector) const {
	return Vector3D ( y*_vector.z - z*_vector.y, z*_vector.x - x*_vector.z, x*_vector.y - y*_vector.x );
}

double Vector3D::dotProduct(const Vector3D &_vector) const {
	return (x*_vector.x) + (y*_vector.y) + (z*_vector.z);
}

Vector3D Vector3D::normalize() const {
	double l = sqrt(x*x + y*y + z*z);

	if (l == 0.0) l = 1.0;
	return (*this) *( 1.0/l );
}

double Vector3D::length() const {
	double l = sqrt(x*x + y*y + z*z);
	return l;
}

double *Vector3D::intoVector() const {
    double *d;
    d = new double[3];
    d[0] = x;
    d[1] = y;
    d[2] = z;

    return d;
}
